//
//  YunCangViewModel.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/7/20.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
import YYModel
import SVProgressHUD
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}


//let OriginalID = "-1"

protocol YunCangViewModelDelegate:NSObjectProtocol {
    func updateDatasource(_ array:NSArray?)
}

class YunCangViewModel: NSObject {
    
    //MARK: - property
    var ArrayMenuData = [[YunCangModel]]()
    var ArrayMenuHeaderData = [YunCangModel]()
    var ArrayMenuFavoriteData = [YunCangModel]()
    
    weak var delegate:YunCangViewModelDelegate?
    //MARK: - coreMothod
    internal func updateData() {
        NetworkTool.sharedTools.postSoap(["Username":userInfo!.userName,"Password":EncryptionTool.encryptString(userInfo!.userPassWord)], bodyParam: ["piOperator":userInfo!.userName], bodyMethod: "GetAllMenu", success: { (responseXML) in
            var headArrayTemp = [YunCangModel]()
            var mueuArrayTemp = [[YunCangModel]]()
            var favoriteArrayTemp = [YunCangModel]()
            for item in analysisManager.shareManager.XmlParserAnalysis(responseXML, elementName:"collect"){
                let model = YunCangModel()
                model.yy_modelSet(with: item)
                if model.FAVORITES == 1{
                    favoriteArrayTemp.append(model)
                }
                if  model.FPARENT == -1 {
                    if mueuArrayTemp.last?.count < 1 && mueuArrayTemp.count > 0{
                        mueuArrayTemp.removeLast()
                        headArrayTemp.removeLast()
                    }
                    headArrayTemp.append(model)
                    mueuArrayTemp.append([YunCangModel]())  //开启新的数组
                }else if  model.RIGHTS == 2{
                        var lastArray = mueuArrayTemp.last!
                        lastArray.append(model)
                        mueuArrayTemp.removeLast()
                        mueuArrayTemp.append(lastArray)
                }
            }
            if mueuArrayTemp.last?.count < 1 &&  headArrayTemp.count > 0 {
                headArrayTemp.removeLast()
                mueuArrayTemp.removeLast()
            }
            self.ArrayMenuHeaderData = headArrayTemp
            self.ArrayMenuData = mueuArrayTemp
            userInfo?.mainMenuDataArray = mueuArrayTemp
            self.ArrayMenuFavoriteData = favoriteArrayTemp
            userInfo?.favoriteItems = favoriteArrayTemp
            //执行代理方法
            if let delegate = self.delegate {
                delegate.updateDatasource(self.ArrayMenuData as NSArray?)
            }
            UserDefaults.standard.set(true, forKey: "Login")
        }) {
            if let delegate = self.delegate {
                delegate.updateDatasource(nil)
                return
            }
        }
    }
    //MARK: - private
    
}
